package com.psl.services.frame;

import com.psl.Dao.Conn;
import com.psl.model.file;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class fileUploadServices {
    public boolean insertfile(String fileName, String fileUUid, String filetype, String size, String order_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uploadTime = format.format(date);
        try {
            pstmt = conn.prepareStatement("insert into filelist(fileName,fileUUid,date,filetype,size,order_id) VALUES (?,?,?,?,?,?)");
            pstmt.setString(1, fileName);
            pstmt.setString(2, fileUUid);
            pstmt.setString(3, uploadTime);
            pstmt.setString(4, filetype);
            pstmt.setString(5, size);
            pstmt.setString(6, order_id);
            pstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public List filelist(int filetype) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List module = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT fileName,fileUUid,date,filetype,size,order_id FROM filelist WHERE filetype=? ORDER by date DESC");
            pstmt.setInt(1, filetype);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                file file = new file();
                file.setFileName(rs.getString(1));
                file.setFileUUid(rs.getString(2));
                file.setDate(rs.getString(3));
                file.setFiletype(rs.getString(4));
                file.setSize(rs.getString(5));
                file.setOrder_id(rs.getString(6));
                module.add(file);
            }
            return module;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public List Orderfilelist(String order_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List module = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT fileName,fileUUid,date,filetype,size,order_id FROM filelist WHERE order_id=?");
            pstmt.setString(1, order_id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                file file = new file();
                file.setFileName(rs.getString(1));
                file.setFileUUid(rs.getString(2));
                file.setDate(rs.getString(3));
                file.setFiletype(rs.getString(4));
                file.setSize(rs.getString(5));
                file.setOrder_id(rs.getString(6));
                module.add(file);
            }
            return module;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public List Orderlist(String order_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List module = new ArrayList();
        try {
            pstmt = conn.prepareStatement("select * from shippingdetails,filelist where filelist.order_id = shippingdetails.order_id and filelist.order_id = ? ");
            pstmt.setString(1, order_id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                file file = new file();
                file.setFileName(rs.getString(1));
                file.setFileUUid(rs.getString(2));
                file.setDate(rs.getString(3));
                file.setFiletype(rs.getString(4));
                file.setSize(rs.getString(5));
                file.setOrder_id(rs.getString(6));
                module.add(file);
            }
            return module;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
